//
//  ChangePwdViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/20.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "AppHeader.h"
#import "LoginViewController.h"
@interface ChangePwdViewController ()

@property (strong, nonatomic) CustomField *userSecurity;

@property(nonatomic,strong) CustomField  *userPassWord;

@property(nonatomic,strong) CustomField  *repeatPWd;

@property(nonatomic,strong) UIButton *btnVerity;

@property(nonatomic,strong) UIButton *sureBtn;

@property(nonatomic,strong) UILabel *userPhone;

@end

@implementation ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setUpUi];
}
-(void)setNav{
    self.title = @"重置密码";
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

//-(void)setUserPhoneNo:(NSString *)userPhoneNo{
//    _userPhoneNo = userPhoneNo;
//    self.userPhone.text = userPhoneNo;
//}

-(void)setUpUi{
  
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *yzm = [[UILabel alloc]init];
    [self.view addSubview:yzm];
    [yzm setTextColor:[UIColor ColorWithHex:@"#5bc2ce"]];
    [yzm setTextAlignment:NSTextAlignmentLeft];
    [yzm setFont:[UIFont systemFontOfSize:16]];
    yzm.text = @"验证码已发送至: +86 ";
    [yzm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(24);
        make.left.equalTo(self.view).offset(20);
    }];
    
    
    self.userPhone  = [[UILabel alloc]init];
    [self.view addSubview:self.userPhone];
    [self.userPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(yzm.mas_centerY);
        make.left.equalTo(yzm.mas_right).offset(10);
    }];
    [self.userPhone setTextColor:[UIColor ColorWithHex:@"#5bc2ce"]];
    [self.userPhone setFont:[UIFont systemFontOfSize:16]];
    self.userPhone.text = self.userPhoneNo;
    

    
    self.userSecurity = [[CustomField alloc]init];
    self.userSecurity.placeholder = @"输入验证码";
    [self.userSecurity setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userSecurity setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userSecurity.layer.cornerRadius = 23;
    self.userSecurity.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userSecurity.layer.borderWidth = 2;
    [self.view addSubview:self.userSecurity];
    [self.userSecurity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(yzm.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    
    UILabel *backLable =[[UILabel alloc]init];
    [self.view addSubview:backLable];
    backLable.backgroundColor = [UIColor ColorWithHex:@"#5ac3d1"];
    [backLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.userSecurity.mas_height);
        make.width.equalTo(@80);
        make.centerY.equalTo(self.userSecurity.mas_centerY);
        make.right.equalTo(self.userSecurity.mas_right).offset(-30);
    }];
    
    
    self.btnVerity = [UIButton new];
    [self.btnVerity setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    self.btnVerity.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.btnVerity setTitleColor:[UIColor ColorWithHex:@"#025d69"] forState:UIControlStateNormal];
    [self.btnVerity.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.btnVerity setBackgroundColor:[UIColor ColorWithHex:@"#5ac3d1"]];
    self.btnVerity.layer.cornerRadius = 23;
    [self.btnVerity addTarget:self action:@selector(sendVerityCodeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnVerity];
    [self.btnVerity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.userSecurity.mas_height);
        make.width.equalTo(@110);
        make.centerY.equalTo(self.userSecurity.mas_centerY);
        make.right.equalTo(self.userSecurity.mas_right);
    }];
    
    self.userPassWord = [[CustomField alloc]init];
    self.userPassWord.placeholder = @"新密码至少6位";
    self.userPassWord.secureTextEntry = YES;
    [self.userPassWord  setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userPassWord  setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userPassWord.layer.cornerRadius = 23;
    self.userPassWord.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userPassWord.layer.borderWidth = 2 ;
    [self.view addSubview:self.userPassWord];
    [self.userPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userSecurity.mas_bottom).offset(40);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    
    self.repeatPWd = [[CustomField alloc]init];
    self.repeatPWd.placeholder = @"再次输入新密码";
    self.repeatPWd.secureTextEntry = YES;
    [self.repeatPWd  setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.repeatPWd  setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.repeatPWd.layer.cornerRadius = 23;
    self.repeatPWd.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.repeatPWd.layer.borderWidth = 2 ;
    [self.view addSubview:self.repeatPWd];
    [self.repeatPWd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPassWord.mas_bottom).offset(40);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    self.sureBtn = [[UIButton alloc]init];
    self.sureBtn.backgroundColor = [UIColor ColorWithHex:@"#5ac3d1"];
    self.sureBtn.layer.cornerRadius = 23;
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.sureBtn addTarget:self action:@selector(registerNetwork) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.repeatPWd.mas_bottom).offset(90);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(@48);
        make.width.equalTo(@340);
    }];

    
}

-(void)registerNetwork{
    NSString *url = [BASEURL stringByAppendingString:kURL_FindPWd];
    
    NSDictionary *dict = @{@"phone":self.userPhoneNo,
                           @"phoneCode":self.userSecurity.text,
                           @"userPassNew":self.userPassWord.text,
                           @"userPassReNew":self.repeatPWd.text};
    NSLog(@"%@",dict);
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0) {
            [CustomProgressHUD showSuccessWithStatus:@"密码修改成功"];
//            self.navigationController popToViewController: animated:<#(BOOL)#>
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error ];
    }];

}
-(void)sendVerityCodeAction{
    NSString *url = [BASEURL stringByAppendingString:kURL_Sendsend];
    NSDictionary *dict = @{@"phone":self.userPhoneNo,
                           @"position":@"findpwd"};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSLog(@"校验了");
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0) {
            [CustomProgressHUD showSuccessWithStatus:@"验证码发送成功!!!"];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
            
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:@"连接失败,请检查您的网络"];
    }];

}

@end
